<?php
/**
 * admin service file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/03
 * Time: 15:20
 */

namespace Modules\Admin\Service;

use HiCommon\Repository\GoodsRepository;
use HiCommon\Repository\RepairstoreGoodsRepository;
use HiCommon\Repository\ShopRepository;
use HiCommon\Repository\ShopUserRepository;
use HiCommon\Service\PublicService;
use Illuminate\Support\Facades\Hash;

class ShopService
{
    /**
     * 获取分页数据
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list($where)
    {
        $with['with'] = ['area_name'];
        $data = ShopRepository::get_list($where, $with);

        $data->appends($where);//添加链接参数
        $list = list_page_data($data, $where);
        $list['province'] = ShopRepository::get_open_province();
        $list['city'] = PublicService::getCityAraeList(['pid' => $where['province']]);//城市
        $list['area'] = PublicService::getCityAraeList(['pid' => $where['city']]);//县

        return $list;
    }

    /**
     * 获取门店详情
     */
    public function get_shop_detail($n_shop_id)
    {
        try {
            $map = ['id' => $n_shop_id];
            $arr_option['with'] = ['province_name', 'city_name', 'area_name', 'checkpoint_service_type', 'type'];
            $detail = ShopRepository::get_one_by_where($map, $arr_option);

            if ($detail) {
                $info = $detail->toarray();
                if (stripos($info['expenses'], '><tbody>')) {
                    $change = ' align="center" width="100%" style="line-height: 35px;"><tbody>';
                    $info['expenses'] = str_replace('><tbody>', $change, $info['expenses']);
                }
                if (stripos($info['expenses'], 'src="/')) {
                    $info['expenses'] = str_replace('src="/', 'src="https://xdminiapi.chxing.cn/', $info['expenses']);
                }
                return ['detail' => $info];
            }
            return ['detail' => []];
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 门店商品列表
     */
    public function admin_shop_goods_list(array $arr_input)
    {
        $arr_input = hi_array_del_null($arr_input);
        try {
            $arr_option['shop'] = get_shop_id();//门店id
            $data = GoodsRepository::get_all($arr_input);

            $data->appends($arr_option);//添加链接参数
            $data = list_page_data($data, $arr_option);

            return hello_success('成功', $data);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 门店服务商品列表
     */
    public function admin_shop_repair_list(array $arr_input)
    {
        $arr_input = hi_array_del_null($arr_input);
        try {
            $data = RepairstoreGoodsRepository::get_list($arr_input);

            $data->appends($arr_input);//添加链接参数
            $list = list_page_data($data, $arr_input);
            return $list;
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 检测手机号或门店名称是否存在
     */
    public function dataMonitoring(array $where)
    {
        $map[$where['type']] = $where['value'];
        $num = ShopRepository::data_monitoring($map, (int)$where['id']);

        if ($num > 0 && $where['type'] == 'name') {
            return hello_error('店铺名称已存在');
        }

        if ($num > 0 && $where['type'] == 'phone') {
            return hello_error('手机号已存在');
        }

        return hello_success('OK');
    }

    /*添加道路救援商铺*/
    public function add_road_help_shop($arr_input)
    {
        //查询手机号是否已注册
        $arr_where = [
            'phone' => $arr_input['phone']
        ];
        $arr_option = [
            'field' => ['id', 'phone']
        ];
        $result = ShopRepository::base_get_one($arr_where, $arr_option);
        if (!is_null($result)) {
            return hello_error('手机号已被注册', [], '');
        }

        //添加商户信息
        $arr_input['status'] = 1;
        $result = ShopRepository::base_add_one($arr_input)->toarray();
        if (empty($result)) {
            return hello_error('添加失败', [], '添加商户信息失败');
        }

        //添加商户主账号信息
        $s_pass = '123456';
        $arr_data = [
            'shop_id' => $result['id'],
            'is_master' => 1,
            'username' => $result['phone'],
            'phone' => $result['phone'],
            'rong_cloud_id' => 'shop_' . uniqid(),
            'pass' => $s_pass,
            'password' => Hash::make($s_pass),
        ];
        $result = ShopUserRepository::base_add_one($arr_data)->toarray();
        if (empty($result)) {
            return hello_error('添加失败', [], '添加商户主账号信息');
        }

        return hello_success('成功');
    }

    /**
     * 商户审核列表
     * @param $where
     * @return array
     */
    public function verify_list($where)
    {
        try {
            if (!empty($where['name'])) {
                $arr_option['where']['name'] = $where['name'];
            }
            if (!empty($where['phone'])) {
                $arr_option['where']['phone'] = $where['phone'];
            }
            if (!empty($where['cate'])) {
                $arr_option['where']['cate'] = $where['cate'];
            }
            $arr_option['where']['status'] = 0;
            $data = ShopRepository::base_get_list($arr_option);

            $data->appends($where);//添加链接参数
            $list = list_page_data($data, $where);

            return hello_success('OK', $list);
        } catch (\Exception $exception) {
            return hello_error($exception->getMessage());
        }

    }

    /**
     * 商户审核详情
     * @param $n_shop_id
     * @return array
     */
    public function get_shop_info($n_shop_id)
    {
        try {
            $map = ['id' => $n_shop_id];
            $arr_option['with'] = ['province_name', 'city_name', 'area_name', 'checkpoint_service_type', 'type'];
            $detail = ShopRepository::get_one_by_where($map, $arr_option);

            if ($detail) {
                $info = $detail->toarray();
                return ['detail' => $info];
            }
            return ['detail' => []];
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 商户审核
     * @param array $data
     * @return array
     */
    public function examine($data = [])
    {
        try {
            if (empty($data['id']) || empty($data['status'])) {
                return hello_error('参数错误');
            }

            $res = ShopRepository::edit_one($data);
            if ($res) {
                return hello_success('审核成功！');
            }

            return hello_error('失败！');
        } catch (\Exception $exception) {
            return hello_error($exception->getMessage());
        }

    }

    /**
     * 是否直营店
     */
    public function change_type($arr_input)
    {
        try {
            $arr_input['id'] = (int)$arr_input['id'];
            if ($arr_input['id'] <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = ShopRepository::edit_one($arr_input);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }


}